package com.summer.edu.controller;


import com.summer.R;
import com.summer.edu.client.VodClient;
import com.summer.edu.entity.Video;
import com.summer.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;
    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video){
        videoService.save(video);
        return R.ok();
    }
    @GetMapping("{id}")
    public R getVideo(@PathVariable String id){
        Video video = videoService.getById(id);
        return R.ok().data("getVideo",video);
    }
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody Video video){
        videoService.updateById(video);
        return R.ok();
    }
      @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
          Video video = videoService.getById(id);
          String sourceId = video.getVideoSourceId();
          if (!StringUtils.isEmpty(sourceId)){
              vodClient.removeAlyVideo(sourceId);
          }
        videoService.removeById(id);
        return R.ok();
      }
}

